<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Homepage extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -  
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
     public function __construct()
     {
		session_start();
        parent::__construct();
		$this->load->model('friendtop_model');
     }
    
     function index()
     {
		$temp=$_SESSION['token']['access_token'];
        //$temp=$this->session->userdata('access_token');
        $update_time=$this->session->userdata("update_time");
        //var_dump($GLOBALS['weibos']);
        //var_dump($temp);
        
        if ( empty($temp))   //如果用户没登陆
        {
			//echo "empty";
            $o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
            $code_url = $o->getAuthorizeURL(WB_CALLBACK_URL);
            echo "<meta http-equiv=refresh content='0; url=$code_url'>";  //跳转到授权页面
        }
        else
        {
			//echo "OK";
            $c = new SaeTClientV2( WB_AKEY , WB_SKEY ,$temp );
			$uid_get=$c->get_uid();   //获取当前登录用户的UID
			$uid=$uid_get['uid'];
			//$user_msg=$c->show_user_by_id($uid);	//根据UID获取当前登录用户的基本信息
			

			//根据当前用户信息获取数据库信息
			$sql="select fids from wb_appFriendsView_custom  where uid=$uid";
			$query_fids=$this->db->query($sql);
			$query=$query_fids->result_array();
			$fid_array=explode("-",(string)$query[0]['fids']);
			$user_info=array();
			
			if( ! empty($fid_array))
			{
				if( !isset($_SESSION['weibos']) || ((strtotime('now') - $update_time)/60)>30)	//如果weibos为空或者距离上次更新时间超过30分钟
				{
					//echo "come again<br>";
					$weibos=array();
					//echo "\$GLOBALS['weibos']";
					foreach($fid_array as $fuid)
					{
						$weibos["$fuid"]=$c->user_timeline_by_id($fuid,1,100);	//取出100条最新微博
					}
					$_SESSION['weibos']=$weibos;
				}
				//var_dump($_SESSION['weibos']);
				//跟据从数据表中取出来的fid分别从数据表wb_user中取出相应用户的信息
				$today=date("d");	//今天的日期（天）
				$count=0;
				foreach($fid_array as $fuid)
				{
					$count_today=0;	//今天更新的微博数量
					//echo $fuid.'<br>';
					
					$weibo=$_SESSION['weibos']["$fuid"];	//取出100条最新微博
					//echo $weibo['statuses'][$count_today]['created_at'].'<br>';
					do
					{
						$creat_time=strtotime($weibo['statuses'][$count_today]['created_at']);	//本条微博发布的时间（天）
						$count_today++;
					}
					while(date("d",$creat_time) == $today);
					
					$query_user=$this->friendtop_model->get_wb_user('wb_user',$fuid);
					//$avatar_url=preg_replace("/50/i", "180", $query_user[0]['profile_image_url']);
					$avatar_url=$query_user[0]['avatar_large'];
					
					$user_info[$count]['uid']=(string)$fuid;	//用户UID
					$user_info[$count]['name']=$query_user[0]['screen_name'];	//用户微博昵称
					$user_info[$count]['img_url']=$avatar_url;	//用户大头像链接
					$user_info[$count]['count']=$count_today-1;	//用户今日更新的微博数量
					
					$count++;	//指向下一个UID
					
				}
            }
            //var_dump($_SESSION['weibos'];
            $data['user_info']=$user_info;
            $this->load->view('friendtop/mainpage_view',$data);
        }
     }
}

/* End of file navigate.php */
/* Location: ./application/controllers/navigate.php */
